Skip to content

tree-sitter-rx: a tree-sitter grammar for .rx - #1

Closed
oddcelot wants to merge 7 commits into
mainfrom
tree-sitter-rx
Closed

oddcelot wants to merge 7 commits into
mainfrom
tree-sitter-rx

Conversation

@oddcelot

@oddcelot oddcelot commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Hi! This adds a tree-sitter grammar for .rx and .d.rx in tooling/tree-sitter-rx.

The language server README mentions that Neovim has no .rx grammar and Zed has no extension. Both need the same thing, so I started here. A Zed extension can follow on top of it.

What you get

  • Highlighting that keeps working while a file is half typed, which is exactly when the language server can't help.
  • Outline, folding, indentation and text objects, even in a folder with no tsconfig.json or node_modules.
  • A way for structural tools like ast-grep to query .rx.

How it works

grammar.js extends tree-sitter-typescript's TSX grammar, so expressions, types and JSX keep the node names you already know from TSX. Each .rx form gets its own node (source_declaration, equation, write_statement, …). The README has the full table.

It differs from TSX in three places, which are the parts most worth a close look:

  • No assignment expressions. A statement-level x = e is always an equation, as the spec says.
  • Where statements end. src/scanner.c is TypeScript's scanner with my changes marked rx:. A newline ends a statement before <, (, [ and a template. On one line, a statement keyword, a write target followed by :=, or a call after a whole expression starts the next statement.
  • Contextual keywords. server, client, error and draft can also be names, as in server = await loadTodos().

Important

compiler/src/parse.rs stays the definition of the language. If the grammar disagrees with it anywhere, that's a bug in the grammar, and I'd love to hear about it.

How I tested it

  • 67 corpus tests in test/corpus
  • All 206 tracked .rx files parse with no ERROR or MISSING node (pnpm run parse:repo)
  • highlights.scm and injections.scm run without query errors, and CSS is injected into <style> bodies
  • The parser builds to WebAssembly with wasi-sdk, the way Zed compiles it
  • A CI job, .github/workflows/tree-sitter.yml: a stale src/ check, the corpus, the repo parse and the queries

What it doesn't do yet

  • A bare word doesn't start a statement on the same line: a = 1 b = 2 parses in parse.rs but not here.
  • A type named ref doesn't parse in type position, where ref is the accessor modifier.
  • The grammar is a bit looser than the compiler in places: JS for and while in handlers, JSX in expression position.
  • highlights.scm is ordered so a later pattern wins (Zed, Neovim). Helix takes the first match and would need the order reversed.

Tip

Don't be alarmed by the diff size. Almost all of it is src/parser.c, which tree-sitter generate writes. It's committed because Zed compiles it from a pinned commit without running the generator. The hand-written part is 17 files.

Thanks for taking a look. Feedback on the node names is especially welcome, since queries will depend on them.

Extends tree-sitter-typescript's TSX grammar. compiler/src/parse.rs stays the reference.
Committed because Zed compiles src/parser.c as it is.
@oddcelot oddcelot mentioned this pull request Nov 18, 2026
5 tasks done
- a signature's return may be an accessor: `): ref string`, `[ref boolean, …]`
- an element's text ends at `<`, `{` and `}` only, so `>` and a bare `&` are text
- a newline before a fn's `{` no longer ends it as a signature
- a `<style>` inside an element is raw CSS
- `import server from "./x"`: a side only before `pure` or `{`
- on one line, a statement keyword, a `use` target and a call start a statement;
  brackets in a string do not count
- a chain of catches folds left
- an attribute's value may be a primary expression without braces: `value=1`
- highlights: a capitalised name is no longer always a type
src/parser.c is committed, so the CLI and the TSX grammar that write it are exact.
…input

- reading ahead on one line, the scanner skips a /* */ comment, and stops at //
- `effect (,) { … }` no longer parses
@oddcelot

Copy link
Copy Markdown
Owner Author

Closing: the grammar landed upstream in solidjs#88 (b470614), and tooling/tree-sitter-rx on main is identical to this branch. The Zed extension (#2) pins its grammar to that commit.

@oddcelot oddcelot closed this Nov 22, 2026
@oddcelot
oddcelot deleted the tree-sitter-rx branch November 22, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant